package com.mycompany.holdemdeckchecker;

import card.entity.Card;
import card.specs.CardName;
import card.specs.CardSuit;
import comparators.ValueComparator;
import holdemdealer.ws.DealerEndpoint;
import java.util.Collections;
import java.util.List;

/**
 * Hello world!
 *
 */
public class App {

    private static final int CARDS_IN_DECK_TOTAL = 14;

    public static void main(String[] args) {

        DealerEndpoint webService = WebServiceConnection.connectToWS();
        

        for (int i = 0; i < 1000; i++) {
            List<Card> hand = webService.getFiveCards();
            ValueComparator valueComparator = new ValueComparator();
            Collections.sort(hand, valueComparator);

            printCards(hand);

            int[] handArray = createHandArray(hand);

            CardName isPair = checkForPair(handArray);
            if (isPair != null) {
                System.out.print("Pair of " + isPair);
            }

            CardName[] isTwoPair = checkForTwoPair(handArray);
            if (isTwoPair != null) {
                System.out.print("Two pairs: " + isTwoPair[0] + " and " + isTwoPair[1]);
            }

            CardName isTrips = checkForTrips(handArray);
            if (isTrips != null) {
                System.out.print("Trips: " + isTrips);
            }

            CardName isStraight = checkForStraight(handArray);
            CardSuit isFlush = checkForFlush(hand);
            if (isStraight != null) {
                if (isFlush != null) {
                    System.out.print("Straight flush: " + isFlush + "-high");
                } else {
                    System.out.print("Straight: " + isStraight + "-high");
                }
            }

            if (isFlush != null) {
                System.out.print("Flush " + isFlush + "-high");
            }

            CardName isCarre = checkForCarre(handArray);
            if (isCarre != null) {
                System.out.print("Carre: " + isCarre);
            }
            System.out.println("");
        }
    }

    private static Card checkForHighCard(List<Card> hand) {
        return hand.get(0);
    }

    private static CardName checkForPair(int[] handArray) {
        for (int i = handArray.length - 1; i > 0; i--) {
            if (handArray[i] == 2) {
                return CardName.fromValue(i);
            }
        }
        return null;
    }

    private static CardName[] checkForTwoPair(int[] handArray) {
        CardName[] result = new CardName[2];
        for (int i = handArray.length - 1; i > 0; i--) {
            if (handArray[i] == 2) {
                result[0] = CardName.fromValue(i);
                for (int j = i - 1; j > 0; j--) {
                    if (handArray[j] == 2) {
                        result[1] = CardName.fromValue(j);
                        return result;
                    }
                }
            }
        }
        return null;
    }

    private static CardName checkForTrips(int[] handArray) {
        for (int i = handArray.length - 1; i > 0; i--) {
            if (handArray[i] == 4) {
                return CardName.fromValue(i);
            }
        }
        return null;
    }

    private static CardName checkForStraight(int[] handArray) {
        for (int i = handArray.length - 1; i > 0; i--) {
            int counter = 1;
            for (int j = i; j > 0; j--) {
                if (handArray[j] == 1) {
                    counter++;
                    if (counter == 5) {
                        return CardName.fromValue(i);
                    }
                } else {
                    counter = 0;
                    break;
                }
            }
        }
        return null;
    }

    private static CardSuit checkForFlush(List<Card> hand) {
        boolean flush = true;
        for (int i = 1; i < hand.size() - 1; i++) {
            if (!hand.get(0).getCardSuit().equals(hand.get(i))) {
                flush = false;
            }
        }
        if (flush) {
            return hand.get(0).getCardSuit();
        } else {
            return null;
        }

    }

    private static CardName checkForCarre(int[] handArray) {
        for (int i = handArray.length - 1; i > 0; i--) {
            if (handArray[i] == 4) {
                return CardName.fromValue(i);
            }
        }
        return null;
    }

    private static int[] createHandArray(List<Card> hand) {
        int[] handArray = new int[CARDS_IN_DECK_TOTAL];
        for (Card card : hand) {
            int cardId = card.getCardName().getCardId();
            handArray[cardId] += 1;
        }
        return handArray;
    }

    private static void printCards(List<Card> hand) {
        for (Card card : hand) {
            System.out.print(card.getCardName() + " " + card.getCardSuit() + ", ");
        }
    }

    private static void printCardArray(int[] handArray) {
        for (int i = 0; i < handArray.length; i++) {
            System.out.print(handArray[i]);
        }
    }
}
